package com.ssy.lingxi.report.dao.platformtemplate;

import com.ssy.lingxi.report.model.bo.MemberShopBO;
import com.ssy.lingxi.report.model.bo.StatisticsNewlyAddedBO;
import com.ssy.lingxi.report.model.vo.request.MemberIdRoleIdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberShopDAO {

    @Autowired
    @Qualifier("platformTemplateJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    public List<MemberShopBO> getMemberShopList(List<MemberIdRoleIdVO> paramList) {
        if (paramList == null || paramList.size() <= 0) {
            return null;
        }

        StringBuilder condition = new StringBuilder(" where ");
        for (MemberIdRoleIdVO item : paramList) {
            condition.append(" (member_id=").append(item.getMemberId()).append(" and role_id=").append(item.getRoleId()).append(") or");
        }
        condition = new StringBuilder(condition.substring(0, condition.length() - 2));

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT ")
                .append(" id,member_id,role_id,logo ")
                .append(" FROM ")
                .append(" pt_member_shop ")
                .append(condition);

        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(MemberShopBO.class));
    }

    /**
     * 统计新增店铺
     * @author 伍将
     * @date 2020/11/12
     * @param startTime: 
     * @param endTime: 
     * @return java.util.List<com.ssy.lingxi.report.model.bo.StatisticsNewlyAddedBO>
     **/
    public List<StatisticsNewlyAddedBO> getNewlyAddedShopByDay(Long startTime, Long endTime) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select ")
                .append(" to_char(to_timestamp(create_time/1000),'YYYY-MM-DD') date_time,count(id)")
                .append(" from ")
                .append(" pt_member_shop ")
                .append(" where create_time between " + startTime + " and " + endTime)
                .append(" group by date_time;");

        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(StatisticsNewlyAddedBO.class));
    }
}
